class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 递增路径的最大长度
     * @param matrix int整型vector<vector<>> 描述矩阵的每个数
     * @return int整型
     */
    bool isuse[1005][1005];
    int remember[1005][1005];
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    int maxroad = 0;
    int n, m;
    int dfs(vector<vector<int> >& matrix, int x, int y) {
        if (isuse[x][y] == true) {
            return remember[x][y];
        }
        for (int k = 0; k < 4; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (nx < 0 || nx >= n || ny < 0 || ny >= m)
                continue;
            if (matrix[x][y] > matrix[nx][ny])
                continue;
            remember[x][y] = max(remember[x][y],
                                 dfs(matrix, nx, ny));
        }
        isuse[x][y] = true;
        return remember[x][y] + 1;
    }

    int solve(vector<vector<int> >& matrix) {
        memset(remember, 0, sizeof remember);
        memset(isuse, 0, sizeof isuse);
        n = matrix.size(), m = matrix[0].size();
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                maxroad = max(maxroad, dfs(matrix, i, j));
            }
        }
        return maxroad + 1;
    }
};